package chemtools_logica.BDL.SFBean;

import chemtools_logica.BDL.IL.BDL_SFEventoLocal;
import chemtools_logica.BDL.IR.BDL_SFEventoRemote;

import chemtools_logica.Entidades.Almacen;
import chemtools_logica.Entidades.Evento;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name = "BDL_SFEvento", mappedName = "map-BDL_SFEvento")
public class BDL_SFEventoBean implements BDL_SFEventoRemote, BDL_SFEventoLocal {
    @Resource
    SessionContext sessionContext;
    @PersistenceContext(unitName = "Chemtools_Logica")
    private EntityManager em;

    public BDL_SFEventoBean() {
    }

    public Evento persistEvento(Evento evento) {
        em.persist(evento);
        return evento;
    }

    public Evento mergeEvento(Evento evento) {
        return em.merge(evento);
    }

    public void removeEvento(Evento evento) {
        evento = em.find(Evento.class, evento.getIdEvento());
        em.remove(evento);
    }

    /** <code>select o from Evento o</code> */
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<Evento> getEventoFindAll() {
        return em.createNamedQuery("Evento.findAll", Evento.class).getResultList();
    }
    
    public List<Evento> getEventosUsuario(){
        List<Evento> listaEvento = new ArrayList<Evento>(); 
        String ejb="SELECT u FROM Evento u WHERE u.idEvento > 9";
        try{
            Query query = em.createQuery(ejb);
            listaEvento = query.getResultList();
        }catch(Exception e)
        {
            listaEvento = null;
        }
        return listaEvento;
    }
    public boolean existeNombreEvento(String nombre){
        boolean exi = false;
     
        String ejb = "SELECT u FROM Evento u WHERE upper(u.descripcion) = '"+nombre.toUpperCase().trim()+"'";
        
        List<Evento> evento = new ArrayList<Evento>();
        Query query = em.createQuery(ejb);
        evento = query.getResultList();
        
        if(evento.size() == 0){
            exi = true;
        }
        
        return exi;
    }
       
}
